package com.gaogzhen.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaogzhen.dto.AdminBankDto;
import com.gaogzhen.mappers.AdminBankDtoMappers;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaogzhen.mapper.AdminBankMapper;
import com.gaogzhen.domain.AdminBank;
import com.gaogzhen.service.AdminBankService;

import java.util.Collections;
import java.util.List;

/**
* @description ${description}
* @author Administrator
* @date 2022-06-06 10:42
* @version 1.0
*/
@Service
public class AdminBankServiceImpl extends ServiceImpl<AdminBankMapper, AdminBank> implements AdminBankService {

    @Override
    public Page<AdminBank> findByPage(Page<AdminBank> page, String bankCard) {
        return page(page, Wrappers.lambdaQuery(AdminBank.class)
                .like(StringUtils.isNotBlank(bankCard), AdminBank::getBankCard, bankCard)
        );
    }

    @Override
    public void add(AdminBank adminBank) {
        boolean save = save(adminBank);
        if (!save) {
            throw new RuntimeException("新增公司银行卡失败！");
        }
    }

    @Override
    public void updateBank(AdminBank adminBank) {
        boolean update = updateById(adminBank);
        if (!update) {
            throw new RuntimeException("修改公司银行卡失败！");
        }
    }

    @Override
    public void updateBankStatus(Long bankId, Integer status) {
        boolean update = update(Wrappers.lambdaUpdate(AdminBank.class)
                .eq(AdminBank::getId, bankId)
                .set(ObjectUtils.isNotNull(status), AdminBank::getStatus, status)
        );
        if (!update) {
            throw new RuntimeException("更新公司银行卡状态失败！");
        }
    }

    @Override
    public List<AdminBankDto> getAllAdminBanks() {
        List<AdminBank> adminBanks = list();
        if (CollectionUtils.isEmpty(adminBanks)) {
            return Collections.emptyList();
        }
        return AdminBankDtoMappers.INSTANCE.toConvertDto(adminBanks);
    }
}
